package com.jinghui.xiaoyuanliao.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.jinghui.xiaoyuanliao.R;
import com.jinghui.xiaoyuanliao.adapter.DrawerAdapter;
import com.jinghui.xiaoyuanliao.bean.ListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends AppCompatActivity {

    private Unbinder bind;
    private RadioGroup mRg;
    private Fragment newsFragment;
    private Fragment linkmanFragment;
    private Fragment otherFragment;
    private DrawerLayout mDL;
    private ListView mListView;
    private List<ListBean> listDatas;
    private DrawerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去标题
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        bind = ButterKnife.bind(this);
        init();//初始化第一个界面,找控件
        initDao();//底部导航栏点击事件
        //抽屉
        //设置数据源
        initDatas();
        //设置适配器
        mAdapter = new DrawerAdapter(listDatas,getApplicationContext());
        //创建适配器创在
        mListView.setAdapter(mAdapter);
        //listView点击事件
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListBean listBean = (ListBean) parent.getItemAtPosition(position);
                String name = listBean.getName();
                if (name != null){
                    switch (name){
                        case "个人资料":
                            Toast.makeText(MainActivity.this, "个人资料", Toast.LENGTH_SHORT).show();
                            break;
                        case "设置":
                            Toast.makeText(MainActivity.this, "设置", Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            }
        });
    }

    private void initDatas() {
        listDatas = new ArrayList<>();
        listDatas.add(new ListBean("个人资料", R.mipmap.ic_launcher));
        listDatas.add(new ListBean("设置", R.mipmap.ic_launcher));
    }

    private void initDao() {
        mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.btn_news:
                        select(0);
                        break;
                    case R.id.btn_linkman:
                        select(1);
                        break;
                    case R.id.btn_other:
                        select(2);
                        break;
                }
            }
        });
    }

    private void init() {
        select(0);
        mRg = (RadioGroup) findViewById(R.id.main_rg);
        mDL = (DrawerLayout) findViewById(R.id.activity_main);
        mListView = (ListView) findViewById(R.id.main_listView);
    }

    private void select(int i) {
        FragmentManager manager = getSupportFragmentManager();//获得fragment管理器
        //开启一个事物
        FragmentTransaction ft = manager.beginTransaction();
        hideFragment(ft);

        switch (i){
            case 0:
                if (newsFragment == null){
                    newsFragment = FragmentFactory.setFragment(i);
                    ft.add(R.id.main_fl,newsFragment);
                }else {
                    ft.show(newsFragment);
                }
                break;
            case  1:
                if (linkmanFragment == null){
                    linkmanFragment = FragmentFactory.setFragment(i);
                    ft.add(R.id.main_fl, linkmanFragment);
                }else {
                    ft.show(linkmanFragment);
                }
                break;
            case  2:
                if (otherFragment == null){
                    otherFragment = FragmentFactory.setFragment(i);
                    ft.add(R.id.main_fl, otherFragment);
                }else {
                    ft.show(otherFragment);
                }
                break;
        }
        ft.commit();

    }

    //隐藏所有的Fragment
    private void hideFragment(FragmentTransaction ft) {
        if (newsFragment != null){
            ft.hide(newsFragment);
        }
        if (linkmanFragment != null){
            ft.hide(linkmanFragment);
        }
        if (otherFragment != null){
            ft.hide(otherFragment);
        }
    }

    @Override
    protected void onDestroy() {
        bind.unbind();
        super.onDestroy();
    }
}
